package com.cskaoyan.service.promote;

import com.cskaoyan.bean.bo.goodsbo.TopicGoodsBO;
import com.cskaoyan.bean.common.BasePageInfoTopic;
import com.cskaoyan.bean.po.MarketGoods;
import com.cskaoyan.bean.po.MarketTopic;
import com.cskaoyan.bean.po.MarketTopicExample;
import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketTopicMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: market-project
 * @BelongsPackage: com.cskaoyan.service.promote
 * @CreateTime: 2022-11-21  15:44
 * @Description: TODO
 * @Version: 1.0
 */
@Service
public class TopicServiceImpl implements TopicService {
    @Autowired
    MarketTopicMapper marketTopicMapper;

    @Autowired
    MarketGoodsMapper marketGoodsMapper;

    @Override
    public PageInfo<MarketTopic> selectByBasePage(BasePageInfoTopic basePageInfoTopic) {
        PageHelper.startPage(basePageInfoTopic.getPage(), basePageInfoTopic.getLimit());

        String title = basePageInfoTopic.getTitle();
        if (title != null) {
            basePageInfoTopic.setTitle(title.trim());
        }

        String sub = basePageInfoTopic.getSubtitle();
        if (sub != null) {
            basePageInfoTopic.setSubtitle(sub.trim());
        }
        //逆向工程查询语句
        MarketTopicExample example = new MarketTopicExample();
        MarketTopicExample.Criteria criteria = example.createCriteria();
        if (basePageInfoTopic.getTitle() != null) {
            criteria.andTitleLike("%" + basePageInfoTopic.getTitle() + "%");
        }
        if (basePageInfoTopic.getSubtitle() != null) {
            criteria.andSubtitleLike("%" + basePageInfoTopic.getSubtitle() + "%");
        }
        example.setOrderByClause(basePageInfoTopic.getSort() + " desc");

        List<MarketTopic> marketTopics = marketTopicMapper.selectByExample(example);
        PageInfo<MarketTopic> marketCouponPageInfo = new PageInfo<>(marketTopics);
        return marketCouponPageInfo;
    }

    @Override
    public void insertByMarketTopic(MarketTopic marketTopic) {
        marketTopicMapper.insertSelective(marketTopic);
    }

    @Override
    public void deleteByIds(ArrayList<Integer> ids) {
        for (Integer id : ids) {
            marketTopicMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public void deleteById(Integer id) {
        marketTopicMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateByMarketTopic(MarketTopic marketTopic) {
        marketTopicMapper.updateByPrimaryKeySelective(marketTopic);
    }

    @Override
    public MarketTopic selectById(Integer id) {
        MarketTopic marketTopic = marketTopicMapper.selectByPrimaryKey(id);
        return marketTopic;
    }

    @Override
    public List<TopicGoodsBO> selectGoodsById(int[] ids) {
        List<TopicGoodsBO> list = new ArrayList<>();
        for (int id : ids) {
            MarketGoods marketGoods = marketGoodsMapper.selectByPrimaryKey(id);
            if (marketGoods == null) {
                return null;
            }
            TopicGoodsBO topicGoodsBO = new TopicGoodsBO();
            topicGoodsBO.setBrief(marketGoods.getBrief());
            topicGoodsBO.setPicUrl(marketGoods.getPicUrl());
            topicGoodsBO.setName(marketGoods.getName());
            topicGoodsBO.setCounterPrice(marketGoods.getCounterPrice());
            topicGoodsBO.setId(marketGoods.getId());
            topicGoodsBO.setNew(marketGoods.getIsNew());
            topicGoodsBO.setRetailPrice(marketGoods.getRetailPrice());
            topicGoodsBO.setHot(marketGoods.getIsHot());

            list.add(topicGoodsBO);
        }
        return list;


    }
}